package com.feng.gameproj;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;

public class ShaderView extends View implements Runnable 
{
	private Bitmap pict = null;
	private int pWidth = 0;
	private int pHeight = 0;
	
	private Paint paint = null;
	
	private BitmapShader bitmapShader = null;
	private ComposeShader composeShader = null;
	private Shader linearShader = null;
	private Shader radialShader = null;
	private Shader sweepShader = null;
	
	private ShapeDrawable shapePict = null;

	public ShaderView(Context context) 
	{
		super(context);
		
		this.pict = BitmapFactory.decodeResource(getResources(), R.drawable.drop);
		this.pWidth = this.pict.getWidth();
		this.pHeight = this.pict.getHeight();
		
		// 创建BitmapShader 对象
		this.bitmapShader = new BitmapShader (this.pict, TileMode.REPEAT, TileMode.MIRROR);
		
		// 创建LinearShader 对象
		int[] colors = new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE};
		this.linearShader = new LinearGradient(0, 0, 100, 100, colors, null, TileMode.REPEAT);
		
		// 用以上两种 Shader对象，创建ComposeShader 对象
		this.composeShader = new ComposeShader (this.bitmapShader, this.linearShader, PorterDuff.Mode.DARKEN);
		
		this.radialShader = new RadialGradient(50, 200, 50, colors, null, TileMode.REPEAT);
		
		this.sweepShader = new SweepGradient(30, 30, colors, null);
		
		this.paint = new Paint ();
		
		new Thread (this).start();
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		
//		this.shapePict = new ShapeDrawable (new OvalShape ());
//		
//		this.shapePict.getPaint().setShader(this.bitmapShader);
//		
//		this.shapePict.setBounds(0, 0, this.pWidth, this.pHeight);
//		
//		this.shapePict.draw(canvas);
		
		this.paint.setShader(this.linearShader);
		canvas.drawRect (this.pWidth, 0, 320, 156, this.paint);
		
		this.paint.setShader(this.composeShader);
		canvas.drawRect(0, 320, this.pWidth, this.pHeight + 300, this.paint);
		
		this.paint.setShader(this.radialShader);
		canvas.drawCircle(50, 200, 50, this.paint);
		
		this.paint.setShader(this.sweepShader);
		canvas.drawRect(150, 160, 300, 300, this.paint);
	}

	@Override
	public void run() 
	{
		while (! Thread.currentThread().isInterrupted())
		{
			try 
			{
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
			postInvalidate ();
		}
	}

}
